package com.tsingteng.cosfun.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.tsingteng.cosfun.R;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/6
 * qq(872179966)
 */

public class TitleView extends FrameLayout implements View.OnClickListener {

    private ImageView mIvLeft;
    private TextView mTvTile;
    private ImageView mIvRight2;
    private OnTitleClickListener onTitleClickListener;
    private Context context;
    private ImageView mIvRight;

    public TitleView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public TitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TitleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_title, null);
        LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        mIvLeft = view.findViewById(R.id.iv_left);
        mTvTile = view.findViewById(R.id.tv_title);
        mIvRight2 = view.findViewById(R.id.iv_right2);
        mIvRight = view.findViewById(R.id.iv_right);
        mIvLeft.setVisibility(GONE);
        mIvRight.setVisibility(GONE);
        mIvRight2.setVisibility(GONE);
        addView(view);

        mIvLeft.setOnClickListener(this);
        mIvRight2.setOnClickListener(this);
        mIvRight.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (onTitleClickListener==null) return;
        switch (v.getId()) {
            case R.id.iv_left:
                onTitleClickListener.onLeft();
                break;
            case R.id.iv_right:
                onTitleClickListener.onRight();
                break;

            case R.id.iv_right2:
                onTitleClickListener.onRight2();
                break;
        }
    }


    public interface OnTitleClickListener{
        void onLeft();
        void onRight();
        void onRight2();
    }

    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener) {
        this.onTitleClickListener = onTitleClickListener;
    }

    public void setIvLeft(int left) {
        if (mIvLeft== null) return;
        mIvLeft.setVisibility(VISIBLE);
        mIvLeft.setImageDrawable(context.getResources().getDrawable(left));
    }

    public void setIvRight(int right) {
        if (mIvRight== null) return;
        mIvRight.setVisibility(VISIBLE);
        mIvRight.setImageDrawable(context.getResources().getDrawable(right));
    }

    public void setIvRight2(int right2) {
        if (mIvRight2== null) return;
        mIvRight2.setVisibility(VISIBLE);
        mIvRight2.setImageDrawable(context.getResources().getDrawable(right2));
    }


    public void setmIvLeftView(int view) {
        if (mIvLeft == null) return;
        mIvLeft.setVisibility(view);
    }

    public void setmIvRightView(int view) {
        if (mIvRight == null) return;
        mIvRight2.setVisibility(view);
    }

    public void setmIvRigh2View(int view) {
        if (mIvRight2 == null) return;
        mIvRight2.setVisibility(view);
    }

    public void setTitle(String title) {
        if (mTvTile== null) return;
        mTvTile.setText(title);
    }
}
